package org.polaris.authority.controller;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.polaris.authority.dto.LoginDTO;
import org.polaris.authority.service.ILoginService;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.BasicMap;
import org.polaris.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Api(value = "登录")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        try {
            Map<String, Object> map = loginService.selectByName(loginDTO);
            return  AjaxResult.result().setObj(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("登录失败   原因:"+e.getMessage());
        }
    }

    @PostMapping("/loginOut")
    public AjaxResult loginOut(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            BasicMap.loginMap.remove(token);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("退出登录失败"+e.getMessage());
        }

    }
}
